function profileRules()
{
	$('#profile').validate({
		rules: {
			email: {
				required: true,
				email: true,
				remote: {
					url: siteUrl + 'user/unique',
					type: 'post',
					data: {
						user_id: function() {
							return $('#user_id').val();
						},
						field: 'email'
					}
				}
			},
			contact_no: {
				required: true,
				number: true
			},
			contact_person: 'required',
                        address: 'required'
		},
		messages: {
			email: {
				required: $.Msgs('required'),
				email: $.Msgs('validEmail'),
				remote: $.Msgs('unique'),
			},
			contact_no: {
				required: $.Msgs('required'),
				number: $.Msgs('integer')
			},
			contact_person: $.Msgs('required'),
			address: $.Msgs('required')
		},
		onkeyup: false,
		focusInvalid: true,
		errorElement: 'span',
		errorPlacement: function(error, element) {
			$(element).next('.icon-error').remove();
			$(element).parent().append('<span class="icon icon-error"></span>');
			var icon = $(element).next('span');
			
			icon.qtip({
				content: $(error).text(),
				position: {
					corner: {
						target: 'rightTop',
						tooltip: 'leftBottom'
					}
				},
				hide: {
					when: 'inactive',
					delay: 1500
				},
				style: 'fielderror'
			});
		},
		highlight: function(element, errorClass) {
			$(element).parent().addClass('ui-state-error ui-corner-all');
		},
		unhighlight: function(element, errorClass) {
			$(element).next('.icon-error').remove();
			$(element).parent().removeClass('ui-state-error ui-corner-all');
		},
		submitHandler: function(form) {
			saveProfile();
		}
	});
}

function saveProfile()
{
	$.loading('Profile');
	
	$.ajax({
		url: siteUrl + 'profile/submit',
		type: 'POST',
		data: $('#profile').serialize(),
		dataType: 'json',
		success: function(response) {

			if (response == true) 
			{
                            window.location = siteUrl + 'profile/';
			}
			
		}
	});
}

function setupChangePassRules()
{
	// check valid old password
	$.validator.addMethod('checkOldPass', function() {
		var valid = false;
		
		$.ajax({
			url: siteUrl + 'profile/check_password',
			type: 'POST',
			data: 'password=' + $('#old_password').val() + '&user_id=' + $('#user_id').val(),
			dataType: 'json',
			success: function(response)	{
				valid = response;
			},
			async: false
		});
		
		return valid;
	}, 'Old password is incorrect.');

	// check if new password and confirm new password match
	$.validator.addMethod('checkNewPass', function() {
		var valid = true;
		
		if($('#new_password').val() != $('#confirm_password').val()) valid = false;
		
		return valid;
	}, $.Msgs('match', '', 'Confirm New Password'));
	
	$('#change_password').validate({
		debug: true,
		rules: {
			old_password: {
				required: true,
				checkOldPass: true
			},
			new_password: {
				required: true,
				minlength: 6,
				maxlength: 32
			},
			confirm_password: {
				required: true,
				checkNewPass: true,
				minlength: 6,
				maxlength: 32
			}
		},
		messages: {
			old_password: {
				required: $.Msgs('required')
			},
			new_password: {
				required: $.Msgs('required'),
				minlength: $.Msgs('minLength','','6'),
				maxlength: $.Msgs('maxLength','','32')
			},
			confirm_password: {
				required: $.Msgs('required'),
				minlength: $.Msgs('minLength','','6'),
				maxlength: $.Msgs('maxLength','','32')
			}
		},
		onkeyup: false,
		focusInvalid: true,
		errorElement: 'span',
		errorPlacement: function(error, element) {
			$(element).next('.icon-error').remove();
			$(element).parent().append('<span class="icon icon-error"></span>');
			var icon = $(element).next('span');
			
			icon.qtip({
				content: $(error).text(),
				position: {
					corner: {
						target: 'rightTop',
						tooltip: 'leftBottom'
					}
				},
				hide: {
					when: 'inactive',
					delay: 1500
				},
				style: 'fielderror'
			});
		},
		highlight: function(element, errorClass) {
			$(element).parent().addClass('ui-state-error ui-corner-all');
		},
		unhighlight: function(element, errorClass) {
			$(element).next('.icon-error').remove();
			$(element).parent().removeClass('ui-state-error ui-corner-all');
		},
		submitHandler: function(form) {
			savePassword();
		}
	});
}

function savePassword()
{
	$.loading('Password');
	
	$.ajax({
		url: siteUrl + 'profile/submit_change_password',
		type: 'POST',
		data: $('#change_password').serialize(),
		dataType: 'json',
		success: function(response) {
			
			if (response == true) 
			{
                            window.location = siteUrl + 'profile/';
                        }
		}
	});
}
